package com.example.demo.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.example.demo.config.delay.DelayQueueManager;
import com.example.demo.config.delay.DelayTask;
import com.example.demo.config.delay.TaskBase;
import com.example.demo.mqtt.MqttProducer;
import com.example.demo.service.ThreadPoolService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.DelayQueue;

/**
 * @Author: wn
 * @Date: 2021/8/3 14:42
 * @Version 1.0
 * @Note
 */
@Slf4j
@Api(tags = "测试")
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private MqttProducer mqttProducer;
    @Autowired
    private DelayQueueManager delayQueueManager;
    @Autowired
    private ThreadPoolService threadPoolService;

    // 测试登录，浏览器访问： http://localhost:8081/user/doLogin?username=zhang&password=123456
    @PostMapping("doLogin")
    public String doLogin(String username, String password) {
        // 此处仅作模拟示例，真实项目需要从数据库中查询数据进行比对
        if("zhang".equals(username) && "123456".equals(password)) {
            StpUtil.login(10001);
            return "登录成功";
        }
        return "登录失败";
    }

    // 查询登录状态，浏览器访问： http://localhost:8081/user/isLogin
    @GetMapping("isLogin")
    public String isLogin(String username, String password) {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }

    @PostMapping("/send")
    public Boolean send(String topic, String data) {
        log.info("开始发送mqtt消息,主题：{},消息：{}", topic, data);
        if (StringUtils.isNotBlank(topic)) {
            mqttProducer.sendToMqtt(data, topic);
            log.info("发送mqtt消息完成,主题：{},消息：{}", topic, data);
            return true;
        } else {
            return false;
        }
    }

    @PostMapping("/sendTest")
    public Boolean sendTest(String topic, String data) {
//        data = "{\n" +
//                "  \"sn\": \"GgbgQo0mRggAl4LxN8Ve\",\n" +
//                "  \"values\": [\n" +
//                "    {\n" +
//                "      \"ts\":\"1667390469770\",\n" +
//                "      \"values\": {\n" +
//                "        \"iccid\": \"89860498102180799157\"\n" +
//                "      }\n" +
//                "    }\n" +
//                "  ]\n" +
//                "}";
//        topic = "v1/gateway/script/telemetry/";
        topic = "/test002";
        for(int i = 0; i < 100; i++) {
            data = String.valueOf(i);
            log.info("开始发送mqtt消息,主题：{},消息：{}", topic, data);
            mqttProducer.sendToMqtt(topic, 1, data);
            log.info("count: {}, 发送mqtt消息完成,主题：{},消息：{}", i, topic, data);
        }

        return true;
    }

    @PostMapping("/delayTest")
    public Boolean delayTest(Integer times) {
        TaskBase taskBase = new TaskBase("1111111111", 1);
        DelayTask delayTask = new DelayTask(taskBase, times * 1000);
        delayQueueManager.put(delayTask);

        return true;
    }

    @PostMapping("/threadPoolTest")
    public Boolean threadPoolTest() throws InterruptedException {
        threadPoolService.F1();
        threadPoolService.F2();

        return true;
    }
}
